home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 078 (1990-06)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 078 (1990-06)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / TrackSalve / TrackSalve / Source / startup.a < prev    next >
Text File  |  1990-06-17  |  6KB  |  209 lines

  1. *
  2. *  Startup.a  -     Copyright 1990 D.W.Reisig
  3. *
  4. *   #     date     by    Comment
  5. *  -- --------- ----- ---------------------
  6. *   0 29-Oct-89     DWR   Created for Tracksalve
  7. *
  8. *  C initial startup procedure under AmigaDOS for usage with Lattice 5.04
  9. *  Special stripped version !!
  10. *
  11.  
  12. *        INCLUDE "exec/types.i"
  13.         INCLUDE "libraries/dosextens.i"
  14.         INCLUDE "call.i"
  15.         INCLUDE "globdefs.i"
  16.  
  17. AbsExecBase    EQU    4
  18.  
  19.  
  20. *-------------------------------------------------------*
  21. *
  22. *  StartUp
  23. *
  24. *  Highest level routine of a CLI-based program, a limited interface between system and application.
  25. *
  26. *  Input:    4(a7)    Stacksize (if called from CLI)
  27. *        a0    Commandline parameter string
  28. *        d0    Commandline parameter length
  29. *
  30. *  Output:    d0    Return value (as can be used in script files)
  31. *        (pr_result2  to pass DOS errors for usage with "Why")
  32. *
  33. *  Usage:    a3    Pointer to our Process structure
  34. *        a4    Pointer to our global data area
  35. *        a5    Stack marker
  36. *        a6    Library pointers
  37. *
  38.  
  39.  
  40.         xdef    _XCEXIT                Exit() is standard way to leave C. (Not here)
  41.  
  42.         xref    _LinkerDB            Linker defined base value
  43.         xref    __BSSBAS            Linker defined base of BSS
  44.         xref    __BSSLEN            Linker defined length of BSS
  45.  
  46.         CSECT    text,0,0,1,2            Any xref's after this are 16-bit reloc
  47.  
  48.         Func    _StartUp
  49.         movem.l d1-d7/a0-a6,-(a7)
  50. REGSIZE        EQU    (7+7)*4
  51.         lea    REGSIZE(a7),A5            Determine old stack pointer, it has some parameters
  52.         move.l    a0,a2                Save commandline parameter pointer
  53.         move.l    d0,d2                And commandline parameter length
  54.  
  55. *--    Set BSS to zero
  56.  
  57.         lea    __BSSBAS,a0            Get base of BSS
  58.         move.l    #__BSSLEN,d0            Get length of BSS in longwords
  59.         moveq.l #0,d1
  60.         bra.s    ClearLoop            And clear for length given
  61. ClearBSS    move.l    d1,(a0)+
  62. ClearLoop    dbf    d0,ClearBSS            Loop..
  63.  
  64.  
  65. *--    Load baseregister and init some importand globals
  66.  
  67.         lea    _LinkerDB,a4            Load base register
  68.         move.l    a7,__StackPtr(a4)        Save stack ptr
  69.         move.l    AbsExecBase.w,a6        Exec pointer
  70.         move.l    a6,_SysBase(A4)
  71.         lea    DOSName(pc),A1            "dos.library"
  72.         moveq.l #0,D0                Version number
  73.         LibCall OpenLibrary            Open DOS-library
  74.         move.l    D0,_DOSBase(A4)            Library base pointer, success?
  75.         bne.s    DOSok                Yes..
  76.         moveq.l #100,d0                No, how did we get here anyway?
  77.         bra    Exit2                Leave..
  78. DOSok
  79.  
  80. *--    Get the address of our task
  81.  
  82.         suba.l    a1,a1                0-> a1 for own task
  83.         LibCall FindTask            Findtask() used as FindProcess()
  84.         move.l    d0,a3                Pointer to struct Process
  85.         move.l    d0,_OurTask(a4)            Save for later and other users
  86.  
  87. *--    Are we running as a child of Workbench?     Leave if so..
  88.  
  89.         move.l    pr_CurrentDir(A3),_curdir(A4)    Lock associated with current directory
  90.         tst.l    pr_CLI(A3)            Pointer to ConsoleLineInterpreter
  91.         bne.s    FromCLI                We are from the CLI, process CLI startup..
  92.         lea.l    pr_MsgPort(a3),a0        Port Workbench will send us it's startupmessage
  93.         LibCall WaitPort            Wait for it
  94.         lea.l    pr_MsgPort(a3),a0        Port Workbench has send us it's startupmessage
  95.         LibCall GetMsg                Remove message from port
  96.         move.l    d0,d3                Keep message
  97.         LibCall Forbid                No task-switching to prevent Workbench to UnLoadSeg() us
  98.         move.l    d3,a1                Workbench startup message
  99.         LibCall ReplyMsg            Reply message to tell Workbench we have terminated
  100.         bra    Exit4                Leave..
  101.  
  102. FromCLI
  103.  
  104. *--    Calculate available stackspace
  105.  
  106.         move.l    a5,D0                Get top of stack
  107.         sub.l    4(a5),D0            Compute bottom
  108.         add.l    #128,D0                Allow for parms overflow
  109.         move.l    D0,__base(A4)            Save for dynamic stack checking
  110.  
  111. *--    Find command name
  112.  
  113.         move.l    pr_CLI(a3),a0            BCPL-pointer to ConsoleLineInterpreter
  114.         add.l    a0,a0                Pointer conversion
  115.         add.l    a0,a0
  116.         move.l    cli_CommandName(a0),a1        BCPL-pointer to name of current command
  117.         add.l    a1,a1                Pointer conversion
  118.         add.l    a1,a1
  119.  
  120. *--    Collect parameters
  121.  
  122.         move.l    d2,d0                Command line parameters length
  123.         moveq.l #0,d1
  124.         move.b    (a1)+,d1            First byte BCPL string is length
  125.         move.l    a1,__ProgramName(A4)        Updated string pointer
  126.         add.l    d1,d0                Add length of command name
  127.         addq.l    #1,d0                Allow for space after command
  128.  
  129.         clr.w    -(A7)                Set null terminator for command line
  130.         addq.l    #1,D0                Force to even number of bytes
  131.         andi.w    #$fffe,D0            Round up
  132.         sub.l    D0,A7                Make room on stack for command line
  133.         subq.l    #2,D0
  134.         clr.w    0(A7,D0)
  135.  
  136. *--    Copy command line onto stack
  137.  
  138.         move.l    d2,d0                Command line parameters length
  139.         subq.l    #1,d0
  140.         add.l    d1,d2                Add length of command name
  141.  
  142. CopyLine    move.b    0(A2,D0.W),0(A7,D2.W)        Copy command line parameters to stack
  143.         subq.l    #1,d2                Post decrement
  144.         dbf    d0,CopyLine            Loop..
  145.         move.b    #' ',0(a7,d2.w)            Add space between command and parms
  146.         subq.l    #1,d2                Post decrement
  147. CopyCommand    move.b    0(a1,d2.w),0(a7,d2.w)        Copy command name to stack
  148.         dbf    d2,CopyCommand            Loop..
  149.         move.l    A7,A1                Commandline address
  150.         move.l    A1,-(A7)            Push command line address
  151.  
  152. *--    Open low level standard error in the current window
  153.  
  154.         lea.l    Asterix(pc),a0            "*" Current window name
  155.         move.l    a0,d1                Name parameter
  156.         move.l    #MODE_OLDFILE,d2        AccessMode parameter
  157.         DosCall Open                Open output
  158.         move.l    d0,_ConOut(a4)            Make public
  159.         beq.s    Exit4                No stderr, leave..
  160.  
  161. *--    Call application or next stage of interface
  162.  
  163. Main        Call    __main                Call C entrypoint
  164.         ;moveq.l #0,d0                Set successful status (we donot: int _main(), main();)
  165.         bra.s    Exit2                Skip exit() stack argument..
  166.  
  167. *--    exit() entry-point.  All our registers are unreliable.
  168.  
  169. _XCEXIT        move.l    4(a7),d0            Extract return code from stack
  170. Exit2        move.l    d0,d7                Keep return code
  171.         lea    _LinkerDB,a4            Base register. Could have been trashed when called Exit()... Yes?
  172.         move.l    __StackPtr(a4),a7
  173. Exit3        ;Call    _MemCleanup            Let's not
  174.  
  175. *--    Close stderr
  176.  
  177.         move.l    _ConOut(a4),d1            File handle to stderr
  178.         DosCall Close                Close stderr
  179.  
  180. Exit4        move.l    _OurTask(a4),a3            Restore a3 as Processpointer
  181.         move.l    _R2(a4),pr_Result2(a3)        Pass some extra error info
  182.         move.l    d7,d0                Return code
  183.         movem.l (a7)+,d1-d7/a0-a6
  184.         rts
  185.  
  186.  
  187. DOSName        dc.b    'dos.library',0
  188. Asterix        dc.b    '*',0
  189.  
  190.  
  191.  
  192. *-------------------------------------------------------*
  193.  
  194.  
  195.         csect    __MERGED,1,,2,2
  196.  
  197.         gs_l    __StackPtr,1
  198.         gs_l    _OurTask,1
  199.         gs_l    _SysBase,1
  200. *        gs_l    _DOSBase,1
  201.         gs_l    _ConOut,1
  202.         gs_l    _R2,1            Secondary result (DOS-Why program)
  203.         gs_l    __base,1        Base of stack
  204.         gs_l    __ProgramName,1
  205.         gs_l    _curdir,1
  206.  
  207.         END
  208.  
  209.